package business;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.ListIterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import orm.CollectivityMonthArrange;
import orm.CollectivityMonthDomain;
import orm.CollectivityMonthDomainAim;
import orm.CollectivityMonthWeekAim;
import orm.CollectivityTeachingPlanMonth;
import orm.IndividualMonthWeekAim;
import orm.IndividualTeachingPlanWeek;
import orm.IndividualWeekArrange;
import orm.IndividualWeekDomain;
import orm.IndividualWeekDomainAim;
import orm.TeachingPlanBaseInfo;

import util.CorePurposeHepler;
import util.DBHelper;
import util.Utility;
import dao.CMonthArrangeDAO;
import dao.CMonthArrangeIDMemoryDAO;
import dao.CMonthDomainAimDAO;
import dao.CMonthDomainDAO;
import dao.CMonthWeekAimDAO;
import dao.CTeachingPlanMonthDAO;
import dao.IMonthWeekAimDAO;
import dao.ITeachingPlanWeekDAO;
import dao.IWeekArrangeDAO;
import dao.IWeekArrangeIDMemoryDAO;
import dao.IWeekDomainAimDAO;
import dao.IWeekDomainDAO;
import dao.StudentDAO;
import dao.TeacherDAO;
import dao.TeachingPlanBaseInfoDAO;


public class ITeachingPlanWeekServlet extends BaseServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public ITeachingPlanWeekServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String strFunc = request.getParameter("func");
		
		if (strFunc == null) {
			return;
		} else if (strFunc.equals("load")) {
			if (request.getAttribute("tpid") == null) {
				com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
				Integer teacherID = teacher.getTeacherId();
				String teacherName = teacher.getTname();
				StudentDAO studentDAO = new StudentDAO();
				request.setAttribute("teacherName", teacherName);
				Hashtable<Integer, String> ht = null;
				try {
					ht = studentDAO.getStudentsByIndiTeacherID(teacherID);
				} catch (SQLException e) {
					e.printStackTrace();
				}
				request.setAttribute("Students", ht);
				
				TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
				try {
					ArrayList<TeachingPlanBaseInfo> alMonthThemeName = tpbiDAO.getMonthThemeNameByTPProperty();
					ArrayList<TeachingPlanBaseInfo> alWeekThemeName = tpbiDAO.getWeekThemeNameByTPProperty();
					
					request.setAttribute("MonthThemeNameArr", alMonthThemeName);
					request.setAttribute("WeekThemeNameArr", alWeekThemeName);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				request.getRequestDispatcher("../" + Utility.JSP_IndividualTeachingPlanWeek).forward(request, response);
			} else {
				
			}
		} else if (strFunc.equals("fresh")) {
			String executionDate = request.getParameter("dateValue");
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();
			
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			try {
				
				ArrayList<TeachingPlanBaseInfo> alITPID = tpbiDAO.getICTPIDByExecutionDate(executionDate, teacherID);
				
				Iterator it = alITPID.iterator();
				
				response.setContentType("text/xml;charset=utf-8");
				StringBuffer sb = new StringBuffer();

				sb.append("<weekDatas>");
				
				Map<String,StringBuffer> datePlanMapping = new LinkedHashMap<String,StringBuffer>();
				
				StringBuffer weekDateNode = null;
				String currentDate = null;
				for(int i = 0; it.hasNext(); i++){
					TeachingPlanBaseInfo tpbi = (TeachingPlanBaseInfo)it.next();
					if(datePlanMapping.get(tpbi.getExecutionDate())==null){
						if(i!=0){
							datePlanMapping.get(currentDate).append("</weekData>");
						}
						weekDateNode = new StringBuffer("<weekData name=\"")
											.append(getDateOfWeek(executionDate,tpbi.getExecutionDate())+1)
											.append("\">");
						datePlanMapping.put(tpbi.getExecutionDate(), weekDateNode);
					}else{
						weekDateNode = datePlanMapping.get(tpbi.getExecutionDate());
					}
					
					weekDateNode.append("<tp>");
					weekDateNode.append("<TPID>"+ tpbi.getTpID() +"</TPID>");
					weekDateNode.append("<ActivityName>" + tpbi.getActivityName() + "</ActivityName>");
					weekDateNode.append("<CorePurpose>" + CorePurposeHepler.getCorePurposeName(tpbi.getCorePurpose()) + "</CorePurpose>");
					weekDateNode.append("</tp>");
					
					currentDate = tpbi.getExecutionDate();
				}
				int oldLength = sb.length();
				for(StringBuffer ss:datePlanMapping.values()){
					sb.append(ss);
				}
				if(sb.length()>oldLength)
					sb.append("</weekData>");
				
				sb.append("</weekDatas>");
				response.getWriter().write(sb.toString());
				response.getWriter().flush();
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
		} else if (strFunc.equals("save")) {
			IndividualTeachingPlanWeek itpw = new IndividualTeachingPlanWeek();
			ArrayList<IndividualWeekDomain> alIWD = new ArrayList<IndividualWeekDomain>();
			ArrayList<IndividualWeekDomainAim> alIWDA = new ArrayList<IndividualWeekDomainAim>();
			ArrayList<IndividualWeekArrange> alIWA = new ArrayList<IndividualWeekArrange>();
			ArrayList<IndividualMonthWeekAim> alIMWA = new ArrayList<IndividualMonthWeekAim>();
			
			ITeachingPlanWeekDAO itpwDAO = new ITeachingPlanWeekDAO();
			IWeekDomainDAO iwdDAO = new IWeekDomainDAO();
			IWeekDomainAimDAO iwdaDAO = new IWeekDomainAimDAO();
			IWeekArrangeDAO iwaDAO = new IWeekArrangeDAO();
			IMonthWeekAimDAO imwaDAO = new IMonthWeekAimDAO();
			TeacherDAO teacherDAO = new TeacherDAO();
			
			pickupFormData(itpw, request);
			getIndividualWeekDomainInsert(alIWD,request);
			getIndividualWeekDomainAimInsert(alIWDA,request);
			getIndividualWeekArrangeInsert(alIWA,request);
			getIndividualMonthWeekAimInsert(alIMWA,request);
			
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();
			String teacherName = teacher.getTname();
			
			itpw.setTeacherID(teacherID);
//			ctpm.setTeacherID(teacherID);
			insertData(itpw, alIWD, alIWDA, alIWA, alIMWA);
			gotoiptwInfo(request, response);
			
//			request.getRequestDispatcher("../" + Utility.JSP_ITeachingPlanWeekInfo).forward(request, response);
			response.sendRedirect("ITeachingPlanWeekArrangeFunction?func=toFavor");
			return;
		} else if (strFunc.equals("edit")) {
			String strIWeekArrangeID = request.getParameter("IWeekArrangeID");
			
			IndividualTeachingPlanWeek itpw = new IndividualTeachingPlanWeek();
			ArrayList<IndividualWeekDomain> alIWD = new ArrayList<IndividualWeekDomain>();
			ArrayList<IndividualWeekDomainAim> alIWDA = new ArrayList<IndividualWeekDomainAim>();
			ArrayList<IndividualWeekArrange> alIWA = new ArrayList<IndividualWeekArrange>();
			ArrayList<IndividualMonthWeekAim> alIMWA = new ArrayList<IndividualMonthWeekAim>();
			TeacherDAO teacherDAO = new TeacherDAO();
			
			pickupFormData(itpw, request);
			getIndividualWeekDomainInsert(alIWD,request);
			getIndividualWeekDomainAimInsert(alIWDA,request);
			getIndividualWeekArrangeInsert(alIWA,request);
			getIndividualMonthWeekAimInsert(alIMWA,request);
			
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			Integer teacherID = teacher.getTeacherId();
			itpw.setTeacherID(teacherID);

			updateData(itpw, alIWD, alIWDA, alIWA, alIMWA, strIWeekArrangeID);
			
			gotoiptwInfo(request, response);
			response.sendRedirect("ITeachingPlanWeekArrangeFunction?func=toFavor");
//			request.getRequestDispatcher("../" + Utility.JSP_ITeachingPlanWeekInfo).forward(request, response);
			
		} else if (strFunc.equals("printITPWeek")) {
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();
			String teacherName = teacher.getTname();
			String iWeekArrangeID = request.getParameter("IWeekArrangeID");
			IndividualMonthWeekAim[] imwa = null;
			
			ITeachingPlanWeekDAO itpwDAO = new ITeachingPlanWeekDAO();
			IWeekDomainDAO iwdDAO = new IWeekDomainDAO();
			IWeekDomainAimDAO iwdaDAO = new IWeekDomainAimDAO();
			IWeekArrangeDAO iwaDAO = new IWeekArrangeDAO();
			IMonthWeekAimDAO imwaDAO = new IMonthWeekAimDAO();
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			
			try {
				IndividualTeachingPlanWeek itpw = itpwDAO.getitpwonthByCMAID(iWeekArrangeID);
				ArrayList<IndividualWeekDomain> alIWD = iwdDAO.getiwdByCMAID(iWeekArrangeID);
				ArrayList<IndividualWeekDomainAim> alIWDA = iwdaDAO.getiwdaArrByCWAID(iWeekArrangeID);
				ArrayList<IndividualWeekArrange> alIWA = iwaDAO.getiwaByiwaID(iWeekArrangeID);
				ArrayList<IndividualMonthWeekAim> alIMWA = imwaDAO.getimwaByCWAID(iWeekArrangeID);
				ArrayList<TeachingPlanBaseInfo> alMonthThemeName = tpbiDAO.getMonthThemeNameByTPProperty();
				ArrayList<TeachingPlanBaseInfo> alWeekThemeName = tpbiDAO.getWeekThemeNameByTPProperty();
				
				String strTPID = "";
				String tpid = null;
				
				imwa = new IndividualMonthWeekAim[alIMWA.size()];
				for(int k = 0; k < alIMWA.size(); k++){
					String iTPIDS = "";
					strTPID = alIMWA.get(k).getTPID();
					String[] alTPID = strTPID.split(",");
					if(alTPID != null){
						for(int m = 0;m < alTPID.length; m++){
							tpid = alTPID[m];
							String temp = tpbiDAO.getThemeActivityNameByTPID(tpid);
							if(temp != null && !temp.equals("")){
								if(!iTPIDS.equals("")){
									iTPIDS+=",";
								}
								iTPIDS += temp;
							}
						}
					}
					imwa[k] = new IndividualMonthWeekAim();
					imwa[k].setTPIDName(iTPIDS);
				}
				
				
				String[] strArrApproStu = request.getParameterValues("AppropriateStudents");
				StringBuffer sb = new StringBuffer();
				StringBuffer sbName = new StringBuffer();
				StudentDAO studentDAO = new StudentDAO();
				
				Hashtable<Integer, String> ht = null;
				try {
					ht = studentDAO.getStudentsByIndiTeacherID(teacherID);
				} catch (SQLException e) {
					e.printStackTrace();
				}
				if(strArrApproStu != null){
					for (int i = 0; i < strArrApproStu.length; i++) {
						sb.append(strArrApproStu[i]);
						if (i != strArrApproStu.length -1) {
							sb.append(",");
						}
						
						if(strArrApproStu[i]!=null && !strArrApproStu[i].equals("")){
							
							sbName.append(ht.get(new Integer(strArrApproStu[i])));
						}
						
//						sbName.append(ht.get(strArrApproStu[i]) );
						if (i != strArrApproStu.length -1) {
							sbName.append(",");
						}
					}
				}
				
				
				itpw.setAppropriateStudents(sb.toString());
				itpw.setAppropriateStudentNames(sbName.toString());
				
				
				
				
				
				request.setAttribute("ITPW", itpw);
				request.setAttribute("IWDArr", alIWD);
				request.setAttribute("IWDAArr", alIWDA);
				request.setAttribute("IWAArr", alIWA);
				request.setAttribute("IMWAArr", alIMWA);
				request.setAttribute("IMWANames", imwa);
				request.setAttribute("MonthThemeNameArr", alMonthThemeName);
				request.setAttribute("WeekThemeNameArr", alWeekThemeName);
				request.setAttribute("IWeekArrangeID", iWeekArrangeID);
					
				request.getRequestDispatcher("../" + 
							Utility.JSP_PrintITPWeek).forward(request, response);
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if(strFunc.equals("toFavor")){
			
			gotoiptwInfo(request, response);
			request.getRequestDispatcher("../" + Utility.JSP_ITeachingPlanWeekInfo).forward(request, response);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	public void pickupFormData(IndividualTeachingPlanWeek itpw, HttpServletRequest request) {	
		com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
		
		Integer teacherID = teacher.getTeacherId();
		String teacherName = teacher.getTname();
		
		String ddddd = request.getParameter("ExecutionDate");
		itpw.setExecutionDate(request.getParameter("ExecutionDate"));
		itpw.setExecutionWeek(request.getParameter("ExecutionWeek"));
		itpw.setMonthThemeName(request.getParameter("MonthThemeName"));
		itpw.setWeekThemeName(request.getParameter("WeekThemeName"));
//		itpw.setAppropriateStudents(request.getParameter("AppropriateStudents"));
		
		String[] strArrApproStu = request.getParameterValues("AppropriateStudents");
		StringBuffer sb = new StringBuffer();
		StringBuffer sbName = new StringBuffer();
		StudentDAO studentDAO = new StudentDAO();

		Hashtable<Integer, String> ht = null;
		try {
			ht = studentDAO.getStudentsByTeacherID(teacherID);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(strArrApproStu != null){
			for (int i = 0; i < strArrApproStu.length; i++) {
				sb.append(strArrApproStu[i]);
				if (i != strArrApproStu.length -1) {
					sb.append(",");
				}
				sbName.append(ht.get(strArrApproStu[i]) );
				if (i != strArrApproStu.length -1) {
					sbName.append(",");
				}
			}
		}
		itpw.setAppropriateStudents(sb.toString());
		itpw.setAppropriateStudentNames(sbName.toString());
		
		
		
		
		itpw.setTeacherID(teacherID);
		itpw.setTeacherName(teacherName);
		itpw.setArrangeDate(request.getParameter("ArrangeDate"));
		itpw.setStarPushDate(request.getParameter("StarPushDate"));
		itpw.setEndPushDate(request.getParameter("EndPushDate"));
		itpw.setEnforceTeacherName(request.getParameter("EnforceTeacherName"));

	}
	
	public void getIndividualWeekDomainInsert(ArrayList<IndividualWeekDomain> iwdArr, HttpServletRequest request){
		for(int domainID = 1; domainID < 8; domainID++){
			IndividualWeekDomain iwd = new IndividualWeekDomain();
			String iWeekArrangeID = request.getParameter("IWeekArrangeID");
			iwd.setIWeekArrangeID(iWeekArrangeID);
			iwd.setDomainID(domainID);
			
			iwd.setRemark(request.getParameter("Remark" + domainID));
			iwdArr.add(iwd);
		}
	}

	public void getIndividualWeekDomainAimInsert(ArrayList<IndividualWeekDomainAim> iwdaArr, HttpServletRequest request){
		for(int domainID = 1; domainID < 8; domainID++){
			
			for(int weekAimID = 1; weekAimID < 6; weekAimID++){
				IndividualWeekDomainAim iwda = new IndividualWeekDomainAim();
				String iWeekArrangeID = request.getParameter("IWeekArrangeID");
				iwda.setIWeekArrangeID(iWeekArrangeID);
				iwda.setDomainID(domainID);
				
				iwda.setWeekAimID(weekAimID);
				iwda.setWeekAimDescribe(request.getParameter("WeekAimDescribe"+domainID+weekAimID));
				
				iwdaArr.add(iwda);
			}
		}
	}
	
	public void getIndividualWeekArrangeInsert(ArrayList<IndividualWeekArrange> iwaArr, HttpServletRequest request){
		for(int weekID = 1; weekID < 6; weekID++){
			IndividualWeekArrange iwa = new IndividualWeekArrange();
			String iWeekArrangeID = request.getParameter("IWeekArrangeID");
			iwa.setIWeekArrangeID(iWeekArrangeID);
			iwa.setWeekID(weekID);
			
			iwa.setEnforceTime(request.getParameter("EnforceTime" + weekID));
			iwa.setEndEnforceTime(request.getParameter("EndEnforceTime" + weekID));
			int trueWeekID = weekID + 10;
			iwa.setRemark(request.getParameter("Remark" + trueWeekID));
			iwaArr.add(iwa);
		}
	}

	public void getIndividualMonthWeekAimInsert(ArrayList<IndividualMonthWeekAim> imwaArr, HttpServletRequest request){
		for(int weekID = 1; weekID < 6; weekID++){
			
			for(int weekAimID = 1; weekAimID < 6; weekAimID++){
				IndividualMonthWeekAim imwa = new IndividualMonthWeekAim();
				String iWeekArrangeID = request.getParameter("IWeekArrangeID");
				imwa.setIWeekArrangeID(iWeekArrangeID);
				imwa.setWeekID(weekID);
				
				imwa.setWeekAimID(weekAimID);
				String[] strTPID = request.getParameterValues("TPID"+weekID+weekAimID);
				StringBuffer sb = new StringBuffer();
				if(strTPID != null){
					for(int i = 0; i < strTPID.length; i++){
						sb.append(strTPID[i]);
						if(i != strTPID.length -1){
							sb.append(",");
						}
					}
				}
				imwa.setTPID(sb.toString());
//				imwa.setTPID(request.getParameter("TPID"+weekID+weekAimID));
				
				imwa.setCorePurpose(request.getParameter("CorePurpose"+weekID+weekAimID));
				
				imwaArr.add(imwa);
			}
		}
	}

	public void insertData(IndividualTeachingPlanWeek itpw, ArrayList<IndividualWeekDomain> iwdArr, 
			ArrayList<IndividualWeekDomainAim> iwdaArr, ArrayList<IndividualWeekArrange> iwaArr, 
			ArrayList<IndividualMonthWeekAim> imwaArr) {
		Connection conn = DBHelper.getConnection();
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
			conn.setAutoCommit(false);
			
			IWeekArrangeIDMemoryDAO iwaIDMemDAO = new IWeekArrangeIDMemoryDAO();
			int nIWeekArrangeID = iwaIDMemDAO.increaseIWeekArrangeID(stmt);
			String iWeekArrangeID = "IWeek" + nIWeekArrangeID;
			
			ITeachingPlanWeekDAO itpwDAO = new ITeachingPlanWeekDAO();
			itpw.setIWeekArrangeID(iWeekArrangeID);
			itpwDAO.insertITPWeek(stmt, itpw);
			
			IWeekDomainDAO iwdDAO = new IWeekDomainDAO();
			iwdDAO.insertIWeekDomain(stmt, iwdArr, iWeekArrangeID);
			
			IWeekDomainAimDAO iwdaDAO = new IWeekDomainAimDAO();
			iwdaDAO.insertIWeekDomainAim(stmt, iwdaArr, iWeekArrangeID);
			
			IWeekArrangeDAO iwaDAO = new IWeekArrangeDAO();
			iwaDAO.insertIWeekArrange(stmt, iwaArr, iWeekArrangeID);
			
			IMonthWeekAimDAO imwaDAO = new IMonthWeekAimDAO();
			imwaDAO.insertIMonthWeekAim(stmt, imwaArr, iWeekArrangeID);

			conn.commit();
			
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException ee) {
				ee.printStackTrace();
			}
		}
	}
	
	public void updateData(IndividualTeachingPlanWeek itpw, ArrayList<IndividualWeekDomain> iwdArr, 
			ArrayList<IndividualWeekDomainAim> iwdaArr, ArrayList<IndividualWeekArrange> iwaArr, 
			ArrayList<IndividualMonthWeekAim> imwaArr, String iWeekArrangeID) {
		Connection conn = DBHelper.getConnection();
		Statement stmt = null;
		
		try {
			conn.setAutoCommit(false);
			stmt = conn.createStatement();
			
			ITeachingPlanWeekDAO itpwDAO = new ITeachingPlanWeekDAO();
			itpw.setIWeekArrangeID(iWeekArrangeID);
			itpwDAO.updateITPWeek(stmt, itpw);
			
			IWeekDomainDAO iwdDAO = new IWeekDomainDAO();
			iwdDAO.updateIWeekDomain(stmt, iwdArr, iWeekArrangeID);
			
			IWeekDomainAimDAO iwdaDAO = new IWeekDomainAimDAO();
			iwdaDAO.updateIWeekDomainAim(stmt, iwdaArr, iWeekArrangeID);
			
			IWeekArrangeDAO iwaDAO = new IWeekArrangeDAO();
			iwaDAO.updateIWeekArrange(stmt, iwaArr, iWeekArrangeID);
			
			IMonthWeekAimDAO imwaDAO = new IMonthWeekAimDAO();
			imwaDAO.updateIMonthWeekAim(stmt, imwaArr, iWeekArrangeID);
			
			conn.commit();
			
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException ee) {
				
			}
		}
	}
	
	public void gotoiptwInfo(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException{
		ITeachingPlanWeekDAO itpwDAO = new ITeachingPlanWeekDAO();
		IndividualTeachingPlanWeek itpw = null;
		TeacherDAO teacherDAO = new TeacherDAO();
		
		com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
		
		Integer teacherID = teacher.getTeacherId();

		try {
			request.setAttribute("pageNo", 1);
			int PageSize = Utility.PageSize;
			ArrayList al = itpwDAO.getAllITeachingPlanWeek(teacherID);
			int totalPage = 0;
			if (al.size() % PageSize == 0) {
				totalPage = al.size()/PageSize;
				request.setAttribute("totalPage",totalPage);
			} else {
				totalPage = (al.size()/PageSize) + 1;
				request.setAttribute("totalPage",totalPage);
			}
	
			al = new ArrayList<IndividualTeachingPlanWeek>();
			ListIterator<IndividualTeachingPlanWeek> it = itpwDAO.getAllITeachingPlanWeek(teacherID).listIterator();
			int ii = 0;
			while (it.hasNext()) {
				IndividualTeachingPlanWeek t = it.next();
				
				itpw = new IndividualTeachingPlanWeek();
				itpw.setIWeekArrangeID(t.getIWeekArrangeID());
				itpw.setMonthThemeName(t.getMonthThemeName());
				itpw.setWeekThemeName(t.getWeekThemeName());
				itpw.setEnforceTeacherName(t.getEnforceTeacherName());
				itpw.setStarPushDate(t.getStarPushDate());
				itpw.setEndPushDate(t.getEndPushDate());
				
				al.add(itpw);
				ii++;

				if(ii > (PageSize-1)) {
					break;
				}
			}
			
			request.setAttribute("ITPWList", al);	
		} catch (SQLException e) {
			e.printStackTrace();
		}	
	}
	
	public static int getDateOfWeek(String monthday,String currentday){
		if(monthday.indexOf(" ")!=-1)
			monthday = monthday.substring(0, monthday.indexOf(" "));
		if(currentday.indexOf(" ")!=-1)
			currentday = currentday.substring(0, currentday.indexOf(" "));
		String[] dataParts1 = monthday.split("-");
		String[] dataParts2 = currentday.split("-");
		Calendar c = Calendar.getInstance();
		c.set(new Integer(dataParts1[0]), new Integer(dataParts1[1])-1, new Integer(dataParts1[2]));
		Calendar c2 = Calendar.getInstance();
		c2.set(new Integer(dataParts2[0]), new Integer(dataParts2[1])-1, new Integer(dataParts2[2]));
		long ss = c2.getTime().getTime() - c.getTime().getTime();
		double dd = ss;
		return dd/1000/60/60/24>ss/1000/60/60/24?((int)ss/1000/60/60/24)+1:((int)ss/1000/60/60/24);
	}

}
